/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.view.renderer
{
	import flash.events.Event;
	
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.PlaylistVO;
	import multimedia.model.vo.UserVO;
	
	import mx.controls.CheckBox;

	public class PlaylistCheckboxRenderer extends CheckBox
	{
		private var model:ModelLocator = ModelLocator.getInstance();
		
		public function PlaylistCheckboxRenderer()
		{
			super();
			addEventListener(Event.CHANGE, onSelectionChange);
		}
		
		override public function set data(value:Object):void
		{
			super.data = value;
			if(model.userFormUser != null)
			{
				selected = model.userFormUser.hasPlaylist(value as PlaylistVO);
			}
		}
		
		private function onSelectionChange(event:Event):void
		{
			if((event.currentTarget as PlaylistCheckboxRenderer).selected)
			{
				model.userFormUser.playlists.addItem(super.data as PlaylistVO);
			} else
			{
				for(var i:int = 0; i < model.userFormUser.playlists.length; i++)
				{
					if((model.userFormUser.playlists.getItemAt(i) as PlaylistVO).playlist_id == (super.data as PlaylistVO).playlist_id)
					{
						model.userFormUser.playlists.removeItemAt(i);
					}
				}
			}
		}
		
	}
}